//
//  MSFoodDetailView.m
//  MSJD
//
//  Created by xiangbo on 2018/9/28.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSFoodDetailPageView.h"
#import "MSCommentheaderView.h"
#import "MSMoreView.h"
#import "MSCommentViewController.h"

@interface MSFoodDetailPageView ()

@end

@implementation MSFoodDetailPageView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame style:UITableViewStyleGrouped];
    if (self) {
        self.tableview.backgroundColor = [UIColor whiteColor];
        [self.tableview registerClass:MSCommentHeaderView.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(MSCommentHeaderView.class)];
        [self.tableview registerClass:MSMoreView.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(MSMoreView.class)];
        [self addSubview:self.topView];
        [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(MS_STATUS_HEIGHT);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(44);
        }];
    }
    return self;
}

#pragma mark Router
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:kMoreButtonClicked]) {
        MSCommentViewController *commentVC = [MSCommentViewController new];
        commentVC.dataSource.data = self.data;
        [[UIViewController currentViewController].navigationController pushViewController:commentVC animated:YES];
    }else if([eventName isEqualToString:kCommentButtonClicked]){
        [MSJumpConn jumpToPunishCommentViewController:self.data];
    }else if([eventName isEqualToString:kMSLoacationExpand]){
        [self.tableview reloadData];
    }else if([eventName isEqualToString:kMSCollectAction]){
        BOOL isCollect = [userInfo[kMSCollectAction] boolValue];
        _WEAKSELF
        [MSCollectionManager collectAction:self.data.mId.stringValue isCollect:isCollect completion:^(BOOL success) {
            if (!success) {
                weakSelf.topView.collectButton.selected = !isCollect;
                [MBProgressHUD showToastWithMessage:@"抱歉,收藏失败,请稍后重试" toView:weakSelf];
            }
        }];
    }else if([eventName isEqualToString:kMSShareAction]){
        
    }
        
    NSLog(@"----------%@----------",eventName);
}
#pragma mark - TableViewDelegate and DataSource
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y <= 0) {
        scrollView.contentOffset = CGPointMake(0, 0);
        self.topView.alpha = 1;
    } else {
        if (scrollView.contentOffset.y < MS_NAVIGATION_HEIGHT) {
            self.topView.alpha = 1 - scrollView.contentOffset.y / MS_NAVIGATION_HEIGHT;
        }else {
            self.topView.alpha = 0;
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *sectionArray = self.dataArray[section];
    return sectionArray.count;
}

- (id)cellData:(NSIndexPath *)indexPath{
    NSMutableArray *sectionArray = self.dataArray[indexPath.section];
    if (indexPath.row < sectionArray.count) {
        return sectionArray[indexPath.row];
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 0;
    id cellData = [self cellData:indexPath];
    if ([cellData isKindOfClass:MSFoodDetailData.class]) {
        if (indexPath.section == 0) {
            height = ((MSFoodDetailData *)cellData).height;
        }else{
            height = 126.f;
        }
    }else if([cellData isKindOfClass:MSCommentlistRowsModel.class]){
        height = ((MSCommentlistRowsModel *)cellData).height;
    }
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    CGFloat height = 0;
    if (section >= 1) {
        NSMutableArray *sectionArray = self.dataArray[section];
        if (sectionArray.count > 0) {
            height = 70;
        }
    }
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    CGFloat height = 0;
    if (section == 1) {
        NSMutableArray *sectionArray = self.dataArray[section];
        if (sectionArray.count > 0) {
            height = 80;
        }
    }
    return height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section >= 1) {
        MSCommentHeaderView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(MSCommentHeaderView.class)];
        NSString *title = section == 1 ? @"精彩点评" : @"附近推荐";
        BOOL hidden = section == 1 ? NO : YES;
        headview.descLabel.text = title;
        headview.commentButton.hidden = hidden;
        headview.lineView.hidden = !hidden;
        return headview;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 1) {
        MSMoreView *footview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(MSMoreView.class)];
        return footview;
    }
    return nil;
}

#pragma mark - Property
- (MSFoodDetailTopView *)topView{
    if (!_topView) {
        _topView = [MSFoodDetailTopView new];
    }
    return _topView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
